<?php

namespace App\Http\Controllers\H5\Shop;

use App\Common\Model\ShopAccount;
use App\Http\Controllers\Controller;
use App\Service\ShopService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    /**
     * 确认兑换页信息
     * @param Request $request
     */
    public function confirmOrderInfo(Request $request, ShopService $shopService)
    {
        $address_id = $request->address_id ?? 0;
        $goods_id = $request->goods_id;
        $uid = $request->uid;
        $res = $shopService->getConfirmOrderInfo($uid, $goods_id, $address_id);
        return ApiResponse($res);

    }

    /**
     * 确认订单
     * @param Request $request
     * @param ShopService $shopService
     * @return array
     */
    public function confirmOrder(Request $request, ShopService $shopService)
    {
        $goodsId = $request->goods_id;
        $amount = $request->amount;
        $uid = $request->uid;
        $addressId = $request->address_id;
        $passwd = $request->passwd;
        $user = ShopAccount::where('uid', $uid)->first();
        $re = $user->checkPasswd($passwd);
        if (!$re) {
            return ApiResponse([], '支付密码错误', 1);
        }
        $orderId = $shopService->createOrder($uid, $goodsId, $amount, $addressId);
        if (!$orderId) return ApiResponse([], $shopService->error, 1);
        return ApiResponse(['order_id' => $orderId]);
    }

    /**
     * 下单成功页面
     * @param Request $request
     */
    public function orderSuccessInfo(Request $request)
    {
        $orderId = $request->order_id;
        $uid = $request->uid;
        $goods = DB::table('order_info')->leftJoin('goods_info', 'order_info.goods_id', '=', 'goods_info.goods_id')
            ->where('order_id', $orderId)->where('order_info.uid', $uid)->select('goods_name', 'order_info.goods_id','num', 'scores','banner', 'goods_info')->first();
        $orderInfo = DB::table('order')->where('id', $orderId)->where('uid', $uid)->first();
        $goodsMain = DB::table('goods')->where('id', $goods->goods_id)->first();
        $return = [
            'order_no' => $orderInfo->order_no,
            'num' => $goods->num,
            'scores' => $orderInfo->scores,
            'receiver' => $orderInfo->receiver,
            'receiver_tel' => $orderInfo->receiver_tel,
            'area' => $orderInfo->area,
            'address' => $orderInfo->address,
            'goods_name' => $goods->goods_name,
            'goods_pic' => json_decode($goods->banner)[0],
            'goods_scores' => $goodsMain->scores,
            'goods_price' => $goodsMain->market_price,
            'freight' => $goodsMain->freight,
            'goods_info' => $goods->goods_info
        ];
        return ApiResponse($return);

    }

    /**
     * 兑换记录(订单列表)
     * @return array
     */
    public function index(Request $request)
    {
         $uid = $request->uid;
         $start = $request->start ?? 0;
         $limit = $request->limit ?? 10;
         $orders = DB::table('order')->leftJoin('order_info', 'order.id', '=', 'order_info.order_id')
            ->select('goods_name', 'order_id', 'order_info.scores', DB::raw('FROM_UNIXTIME(aixin_order.create_time) as create_time'))
             ->where('order.uid', $uid)
             ->offset($start)->limit($limit)
             ->orderBy('order.id', 'desc')
            ->get();
         return ApiResponse($orders);
    }

    /**
     * 兑换详情（订单详情）
     * @param Request $request
     */
    public function show(Request $request)
    {
        $uid = $request->uid;
        $orderId = $request->order_id;
        $orderInfo = (new ShopService())->getOrderDetail($orderId, $uid);
        return ApiResponse($orderInfo);
    }
}
